/**
 * 
 */

package net.messagenet.mazor.web.mngbeans;
import java.io.Serializable;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import net.messagenet.mazor.DO.DpaProductDO;
import net.messagenet.mazor.ejb.facade.MZRFacadeLocal;
import net.messagenet.mazor.exception.MZRInternalServerException;

import org.ajax4jsf.model.KeepAlive;
import org.apache.log4j.Logger;

/**
 * @author Idan
 *
 */
@KeepAlive
public class RefundsManagedBean implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -5388230760485952510L;
	private MZRFacadeLocal facade=null;
	private List<DpaProductDO> prds ;
	private Date fromDate;
	private Date toDate;
	 
	

	private Logger logger = Logger.getLogger(this.getClass());

	

	public Date getFromDate() {
		return fromDate;
	}



	public void setFromDate(Date fromDate) {
		this.fromDate = fromDate;
	}



	public Date getToDate() {
		return toDate;
	}



	public void setToDate(Date toDate) {
		this.toDate = toDate;
	}



	public List<DpaProductDO> getPrds() {
		return prds;
	}



	public void setPrds(List<DpaProductDO> prds) {
		this.prds = prds;
	}



	@PostConstruct
	public void init() {
		
		InitialContext ic;
			
		try {
				ic = new InitialContext();
				facade = (MZRFacadeLocal) ic.lookup("MZRFacade/local");
			} catch (NamingException e) {
				logger.error(e.getMessage(),e);
			}
			prds = new LinkedList<DpaProductDO>();
			

	   }
	/**
	 * 
	 * This methods search the system from DPAs and fills the data in the table
	 * 
	 */
	public void getPrdsBetweenDates(){
		logger.debug("getting prds between dates");
		try {
			prds = facade.getDPAReportedProducts( fromDate,toDate);
			
		}catch (MZRInternalServerException e) {
			logger.error(e.getMessage(), e);
		} 
	}
	public String getCanUpdate(){
		if (prds.isEmpty())
			return "True";
		else
			return "False";
	}
	/**
	 * This method updates payment form the proudcts
	 * 
	 * @return
	 */
	public String update(){
		logger.debug("update refunds");
		try {
			facade.updatePaidProducts( prds);
			
		}catch (MZRInternalServerException e) {
			logger.error(e.getMessage(), e);
			return "opr/failedopr.jsp";
		} 

		return "opr/successsavedata.jsp";
	}

}
